package cn.dckeji.dcssm.common.model;

import cn.dckeji.dcssm.common.enums.ResultCodeEnum;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * @author kenny
 * @Title: ResultModel
 * @ProjectName dcssm
 * @Description: 接口返回结果对象
 * @date 2019/2/21 15:58
 */
@ApiModel(description = "返回结果")
public class ResultModel<T> {

    @ApiModelProperty("是否成功:成功状态码10200")
    private Integer code;
    @ApiModelProperty("描述性原因")
    private String message;
    @ApiModelProperty("业务数据")
    private T data;

    private ResultModel(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static<T> ResultModel<T> success() {
        return new ResultModel(ResultCodeEnum.SUCCESS.code(), "SUCCESS", "");
    }
    public static<T> ResultModel<T> success(T data) {
        return new ResultModel<>(ResultCodeEnum.SUCCESS.code(), "SUCCESS", data);
    }
    public static<T> ResultModel<T> success(String message, T data) {
        return new ResultModel<>(ResultCodeEnum.SUCCESS.code(), message, data);
    }
    public static ResultModel failure() {
        return new ResultModel<>(ResultCodeEnum.FAIL.code(), "FAILURE", null);
    }
    public static ResultModel failure(Integer code, String message) {
        return new ResultModel<>(code, message, null);
    }
    public static ResultModel failure(String message) {
        return new ResultModel<>(ResultCodeEnum.FAIL.code(), message, null);
    }


    public static ResultModel failureS(String message) {
        return new ResultModel<>(10500, message, null);
    }
    public static JSONObject page(Long total, Object records) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total", total);
        jsonObject.put("records", records);
        jsonObject.put("message", "SUCCESS");
        jsonObject.put("code", ResultCodeEnum.SUCCESS.code());
        return jsonObject;
    }
    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
